#include "widget.h"
#include "ui_widget.h"
#include <QIcon>
#include <QSize>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 正确设置目标按钮的图标（QPushButton使用setIcon而非setPixmap）
    QIcon targetIcon(":/hachimitsu.jpg");
    ui->pushButton_target->setIcon(targetIcon);
    // 设置图标大小（根据需要调整，避免过大或过小）
    ui->pushButton_target->setIconSize(QSize(140, 120));  // 修正原代码中1000的不合理高度

    // 方向按钮图标设置
    ui->pushButton_up->setIcon(QIcon(":/up.png"));
    ui->pushButton_down->setIcon(QIcon(":/down.png"));
    ui->pushButton_left->setIcon(QIcon(":/left.png"));
    ui->pushButton_right->setIcon(QIcon(":/right.png"));
    // 统一方向按钮图标大小
    QSize btnSize(30, 30);
    ui->pushButton_up->setIconSize(btnSize);
    ui->pushButton_down->setIconSize(btnSize);
    ui->pushButton_left->setIconSize(btnSize);
    ui->pushButton_right->setIconSize(btnSize);

    // 设置快捷键
    // ui->pushButton_up->setShortcut(QKeySequence("w"));
    // ui->pushButton_down->setShortcut(QKeySequence("s"));
    // ui->pushButton_left->setShortcut(QKeySequence("a"));
    // ui->pushButton_right->setShortcut(QKeySequence("d"));


    // 设置快捷键也可以写作
    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_W));
    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_S));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_A));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_D));


    // 开启重复触发
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);
}

Widget::~Widget()
{
    delete ui;
}

// 上移按钮
void Widget::on_pushButton_up_clicked()
{
    const QRect& rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x(), rect.y() - 5, rect.width(), rect.height());
    qDebug() << "上移";  // 修正调试信息
}

// 下移按钮
void Widget::on_pushButton_down_clicked()
{
    const QRect& rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x(), rect.y() + 5, rect.width(), rect.height());
    qDebug() << "下移";  // 修正调试信息
}

// 左移按钮
void Widget::on_pushButton_left_clicked()
{
    const QRect& rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x() - 5, rect.y(), rect.width(), rect.height());
    qDebug() << "左移";  // 修正调试信息
}

// 右移按钮
void Widget::on_pushButton_right_clicked()
{
    const QRect& rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x() + 5, rect.y(), rect.width(), rect.height());
    qDebug() << "右移";  // 修正调试信息
}
